package com.sql;

import java.awt.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import com.info.GoodsInfo;
import com.info.UserInfo;
import com.mysql.jdbc.Connection;

public class Sql_user extends Init{
	
	public String register(UserInfo user){
		Connection con=(Connection) SqlInit();
		int i=0;
		String sql="insert into user_info(username,password,name,phone,email,question1,answer1,question2,answer2) values(?,?,?,?,?,?,?,?,?)";
		try {
			PreparedStatement preStmt=con.prepareStatement(sql);
			preStmt.setString(1, user.getUsername());
			preStmt.setString(2, user.getPassword());
			preStmt.setString(3, user.getName());
			preStmt.setString(4, user.getPhone());
			preStmt.setString(5, user.getEmail());
			preStmt.setString(6, user.getQuestion1());
			preStmt.setString(7, user.getAnswer1());
			preStmt.setString(8, user.getQuestion2());
			preStmt.setString(9, user.getAnswer2());
			i=preStmt.executeUpdate();
			if(i==1){
				return "success";
			}else{
				return "error";
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	public UserInfo login(String username){
		Connection con=(Connection) SqlInit();
		String sql="select * from user_info where username='"+username+"'";
		try {
			PreparedStatement preStmt=con.prepareStatement(sql);
			ResultSet rs=preStmt.executeQuery();
			UserInfo user=new UserInfo();
			if(rs.next()){
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setEmail(rs.getString("email"));
				user.setQuestion1(rs.getString("question1"));
				user.setAnswer1(rs.getString("answer1"));
				user.setQuestion2(rs.getString("question2"));
				user.setAnswer2(rs.getString("answer2"));
				user.setIntegral(rs.getInt("integral"));
				rs.close();
				con.close();
				return user;
			}else{
				rs.close();
				preStmt.close();
				con.close();
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String saveUserInfo(int id,String name,String Info){
		Connection con=(Connection) SqlInit();
		String sql="update user_info set "+name+"=? where id='"+id+"'";
		try {
			PreparedStatement state=con.prepareStatement(sql);
			state.setString(1, Info);
			int i=state.executeUpdate();
			if(i==1){
				return "success";
			}else{
				return "error";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	public String buyGoods(UserInfo user,GoodsInfo goods){
		Connection con=(Connection) SqlInit();
//		int userId=user.getId();
//		double integral=user.getIntegral();
//		int goodsId=goods.getId();
//		int stock=goods.getStock();
		String sql1="update user_info set integral=? where id='"+user.getId()+"'";
		String sql2="update good_info set sales=?,stock=? where id='"+goods.getId()+"'";
		try {
			PreparedStatement state=con.prepareStatement(sql1);
			state.setDouble(1, user.getIntegral());
			int i=state.executeUpdate();
			if(i==1){
				state=con.prepareStatement(sql2);
				state.setInt(1, goods.getSales());
				state.setInt(2, goods.getStock());
				int k=state.executeUpdate();
				if(k==1){
					return "success";
				}else{
					return "error";
				}
			}else{
				return "error";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	public HashMap goodsInfo(int type){
		Connection con=(Connection) SqlInit();
		String sql="select * from good_info where type='"+type+"' and stock>0";
		HashMap<Integer, GoodsInfo> allGoods=new HashMap<>();
//		ArrayList<GoodsInfo> allGoods=new ArrayList<>();
		try {
			PreparedStatement preStmt = con.prepareStatement(sql);
			ResultSet rs=preStmt.executeQuery();
			while(rs.next()){
				GoodsInfo goods=new GoodsInfo();
				goods.setAddress(rs.getString("address"));
				goods.setComment(rs.getInt("comment"));
				goods.setDes(rs.getString("des"));
				goods.setId(rs.getInt("id"));
				goods.setImg(rs.getString("img"));
				goods.setName(rs.getString("name"));
				goods.setPrice(rs.getDouble("price"));
				goods.setSales(rs.getInt("sales"));
				goods.setStock(rs.getInt("stock"));
				goods.setStore(rs.getString("store"));
				goods.setType(rs.getInt("type"));
				allGoods.put(goods.getId(), goods);
			}
			rs.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return allGoods;
	}
}
